package com.vs.controller;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.vs.common.DuplicateTypeException;
import com.vs.common.SessionKeys;
import com.vs.dao.LaborTypeOperation;
import com.vs.model.CustomMessage;
import com.vs.model.LaborTypeForm;
import com.vs.model.User;
import com.vs.util.Constants;

public class LaborTypeAction extends Action {

	@Override
	public ActionForward execute(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response)
			throws Exception {

		String next = "";
		LaborTypeForm laborTypeForm = (LaborTypeForm) form;
		// System.out.println("labor type=" + laborTypeForm.getLaborType());
		Constants.log.debug(laborTypeForm);

		HttpSession session = getActiveSession(request);
		User user = (User) session.getAttribute(SessionKeys.CURRENT_USER);
		laborTypeForm.setAddedBy(user.getUserName());
		
		ArrayList<CustomMessage> customErrors = new ArrayList<CustomMessage>();
		String action = request.getParameter("action");
		Constants.log.debug("action1=" + action);
		System.out.println("action1=" + action);

		if (action == null) {
			action = laborTypeForm.getAction();
			System.out.println("action2=" + action);
		}

		if (action != null) {
			if (action.equals("selectAll")) {
				next = "success";
			} else if (action.equals("add")) {
				try {
					boolean result = new LaborTypeOperation()
							.addLaborType(laborTypeForm);
					if (result) {
						next = "success";
						CustomMessage info = new CustomMessage();
						info.setMessageLevel(Constants.INFO);
						info.setMessageText("Labor Type "
								+ laborTypeForm.getLaborType()
								+ " added successfully");
						customErrors.add(info);
					} else {
						next = "addError";
						System.out
								.println("labor type action in duplicate exception");
						CustomMessage info = new CustomMessage();
						info.setMessageLevel(Constants.ERROR);
						info.setMessageText("error while adding labor type ");
						customErrors.add(info);
					}
				} catch (DuplicateTypeException e) {
					next = "addError";
					System.out
							.println("labor type action in duplicate exception");
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("Labor Type "
							+ laborTypeForm.getLaborType()
							+ " already present in the system");
					customErrors.add(info);

				}
			} else if (action.equals("delete")) {

				Integer laborTypeId = Integer.parseInt(request
						.getParameter("laborTypeId"));
				boolean result = new LaborTypeOperation()
						.deleteLaborType(laborTypeId);
				if (result) {
					next = "success";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.INFO);
					info.setMessageText("Labor Type " + laborTypeId
							+ " is deleted successfully");
					customErrors.add(info);
				} else {
					next = "deleteError";
					CustomMessage info = new CustomMessage();
					info.setMessageLevel(Constants.ERROR);
					info.setMessageText("error while deleting labor type details");
					customErrors.add(info);
				}
			}
		}

		if (next.equals("success")) {
			updateLaborTypesInSession(request);
		}
		System.out.println("next=" + next);
		request.setAttribute("customErrors", customErrors);
		return mapping.findForward(next);
	}

	private HttpSession getActiveSession(HttpServletRequest request) {
		return request.getSession(false);
	}

	private void updateLaborTypesInSession(HttpServletRequest request) {
		HttpSession session = getActiveSession(request);
		session.removeAttribute("laborTypes");
		session.setAttribute("laborTypes",
				new LaborTypeOperation().getLaborTypeLOV());
	}
}
